﻿$().ready(function () {
    $("#breadcrumb").append('<li><a href="' + $("#urlInventoryLosses").val() + '" title="Faltantes o pérdidas en Inventario">Faltantes o pérdidas en Inventario</a></li>');
    $("#optInventoryLosses").addClass("HoverOnList");

    $("#txtQuantity").spinner({
        min: 0,
        step: 1,
        start: 0,
        numberFormat: "n"
    });

    $('.item-del').live('click', function () {
        if (confirm('¿Esta seguro que desea eliminar el registro?')) {
            $(this).closest('li').remove();
        }
    });

    if (!(typeof $('#txtItemFilter')[0] === "undefined")) {
        InitAutocomplete();
    }

});

function InitAutocomplete() {
    $("#txtItemFilter").autocomplete({
        source: function (request, response) {
            $.ajax({
                type: "POST",
                url: $('#urlGetProducts').val(),
                dataType: "json",
                data: {
                    searchString: request.term,
                    IsByName: $('#rdbNameFilter').is(':checked'),
                    maxRows: 12
                },
                parse: function (data) {
                    var rows = new Array();
                    for (var i = 0; i < data.length; i++) {
                        rows[i] = { data: data[i], value: data[i].name, result: data[i].name };
                    }
                    return rows;
                },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.code + " - " + item.name,
                            ID: item.productID,
                            ProviderID: item.providerID,
                            Code: item.code,
                            Name: item.name,
                            Cost: item.cost,
                            Price: item.price,
                            TotalPrice: item.totalPrice,
                            CurrentExistence: item.currentExistence
                        }
                    }));
                },
                error: function (data, status, e) {
                    alert(e);
                }
            });
        },
        minLength: 0,
        select: function (event, ui) {
            $('#productID').val(ui.item.ID);
        }
    });

    $("#txtItemFilter").keyup(function () {
        if (!this.value) {
            $('#productID').val();
        }
    });
}